package com.wy2cloud.sample.zuulandauthserver.config;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import com.wy2.core.date.DateTime;
import com.wy2cloud.sample.zuulandauthserver.entity.AuthorizeAuthority;
import com.wy2cloud.sample.zuulandauthserver.entity.AuthorizeRole;
import com.wy2cloud.sample.zuulandauthserver.entity.AuthorizeRoleAuthority;
import com.wy2cloud.sample.zuulandauthserver.entity.AuthorizeUser;
import com.wy2cloud.sample.zuulandauthserver.entity.AuthorizeUserRole;
import com.wy2cloud.sample.zuulandauthserver.repository.AuthorizeAuthorityRepository;
import com.wy2cloud.sample.zuulandauthserver.repository.AuthorizeRoleAuthorityRepository;
import com.wy2cloud.sample.zuulandauthserver.repository.AuthorizeRoleRepository;
import com.wy2cloud.sample.zuulandauthserver.repository.AuthorizeUserRepository;
import com.wy2cloud.sample.zuulandauthserver.repository.AuthorizeUserRoleRepository;

@Component
public class WebStartupRunner implements CommandLineRunner {
    
    private static String ADMIN_ACCCOUNT="admin";
    
    private static Logger logger=LoggerFactory.getLogger(WebStartupRunner.class);
    
    @Autowired
    private AuthorizeAuthorityRepository authorizeAuthorityRepository;
    @Autowired
    private AuthorizeRoleRepository authorizeRoleRepository;
    @Autowired
    private AuthorizeRoleAuthorityRepository authorizeRoleAuthorityRepository;  
    
    @Autowired
    private AuthorizeUserRepository authorizeUserRepository;
    
    @Autowired
    private AuthorizeUserRoleRepository authorizeUserRoleRepository;
    
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    public void run(String... arg0) throws Exception {
        // TODO Auto-generated method stub
        
        AuthorizeUser user=authorizeUserRepository.findUserByAccount(ADMIN_ACCCOUNT);
        if(user==null) {
            logger.info("默认管理员不存在，准备创建");
            
            //创建资源
            AuthorizeAuthority authorizeAuthority=new AuthorizeAuthority();
            authorizeAuthority.setId("10");
            authorizeAuthority.setName("图片管理");
            authorizeAuthority.setDescription("图片管理");
            authorizeAuthority.setAuthorityType("1");
            AuthorizeAuthority authorizeAuthority1=new AuthorizeAuthority();
            authorizeAuthority1.setId("11");
            authorizeAuthority1.setName("内容管理");
            authorizeAuthority1.setDescription("内容管理");
            authorizeAuthority1.setAuthorityType("1");
            AuthorizeAuthority authorizeAuthority2=new AuthorizeAuthority();
            authorizeAuthority2.setId("12");
            authorizeAuthority2.setName("下载管理");
            authorizeAuthority2.setDescription("下载管理");
            authorizeAuthority2.setAuthorityType("1");
            
            authorizeAuthorityRepository.save(Arrays.asList(authorizeAuthority,authorizeAuthority1,authorizeAuthority2));
            
            //创建角色
            AuthorizeRole authorizeRole=new AuthorizeRole();
            authorizeRole.setName("默认角色");
            authorizeRole.setStatus(1);
            AuthorizeRole authorizeRole1=new AuthorizeRole();
            authorizeRole1.setName("测试角色");
            authorizeRole1.setStatus(1);
            authorizeRoleRepository.save(Arrays.asList(authorizeRole,authorizeRole1));
            
            //插件默认用户
            user=new AuthorizeUser();
            user.setName(ADMIN_ACCCOUNT);
            user.setAccount(ADMIN_ACCCOUNT);
            user.setBirthday(DateTime.Parse("1980-08-02 00:00:00").getDate());
            user.setEmail("coolmoonf@163.com");
            user.setSex("1");
            user.setPhone("15009315375");
            user.setUserLock(0);
            user.setCreateTime(DateTime.Now().getDate());
            //BCryptPasswordEncoder bc = new BCryptPasswordEncoder(4);
            user.setPassword(bCryptPasswordEncoder.encode("111111"));
            authorizeUserRepository.save(user); 
            
            //插件角色权限资源和用户角色
            final AuthorizeUser tempUser=user;
            List<AuthorizeRole> roles = authorizeRoleRepository.findAll();
            roles.forEach(role->{
            	AuthorizeRoleAuthority authorizeRoleAuthority=new AuthorizeRoleAuthority();
            	authorizeRoleAuthority.setRoleId(role.getId());
            	authorizeRoleAuthority.setAuthorityId("10");
            	AuthorizeRoleAuthority authorizeRoleAuthority1=new AuthorizeRoleAuthority();
            	authorizeRoleAuthority1.setRoleId(role.getId());
            	authorizeRoleAuthority1.setAuthorityId("11");
            	AuthorizeRoleAuthority authorizeRoleAuthority2=new AuthorizeRoleAuthority();
            	authorizeRoleAuthority2.setRoleId(role.getId());
            	authorizeRoleAuthority2.setAuthorityId("12");            	
            	authorizeRoleAuthorityRepository.save(Arrays.asList(authorizeRoleAuthority,authorizeRoleAuthority1,authorizeRoleAuthority2));
            	
            	AuthorizeUserRole authorizeUserRole=new AuthorizeUserRole();
            	authorizeUserRole.setRoleId(role.getId());
            	authorizeUserRole.setUserId(tempUser.getId());
            	authorizeUserRoleRepository.save(authorizeUserRole);
            });
            
            
            
            
            

            
        }

        
    }

}
